package com.example.wyh.intelcheckbash.bug;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.wyh.intelcheckbash.R;
import com.example.wyh.intelcheckbash.common.SysExitUtil;
import com.example.wyh.intelcheckbash.read_json.LocalJsonResolutionUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

//import android.app.SearchManager;
//import android.support.v7.widget.SearchView;
//import android.widget.SearchView.OnQueryTextListener;
import android.widget.SearchView;

/**
 * Created by chen on 17/11/29.
 */

public class CodeInfoActivity extends AppCompatActivity {

    JSONArray mSearchResult;
    android.widget.SearchView mSearchEquipmentCodeView;
    JSONArray mAllCodes;
    ListView mShowCodeListView;
    CodeListAdapter codeListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code_info);
        SysExitUtil.activityList.add(CodeInfoActivity.this);
        initActionBar();
        mShowCodeListView = (ListView)findViewById(R.id.equipment_code_list);
        String fileName = "equipment_info.json";
        final String foodJson = LocalJsonResolutionUtils.getJson(CodeInfoActivity.this, fileName);
        try {
            mAllCodes = new JSONObject(foodJson).getJSONArray("data");
            mSearchResult = mAllCodes;
        }catch (JSONException e)
        {

        }

        codeListAdapter = new CodeListAdapter(CodeInfoActivity.this,mSearchResult);
        mShowCodeListView.setAdapter(codeListAdapter);
        mShowCodeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = layoutInflater.inflate(R.layout.code_item, null);
                try {
                    Intent data = new Intent();
                    data.putExtra("equipment_code", mSearchResult.getJSONObject(i).toString());
                    setResult(1000, data);
                    CodeInfoActivity.this.finish();
                }catch (JSONException e)
                {

                }

            }
        });

        mSearchEquipmentCodeView = (android.widget.SearchView)findViewById(R.id.search_equipment_code);
        mSearchEquipmentCodeView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                android.widget.SearchView searchView = (android.widget.SearchView)view;
            }
        });

        mSearchEquipmentCodeView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            public boolean onQueryTextSubmit(String query) {
                return true;
            }

            public boolean onQueryTextChange(String newText) {
                if (newText != null && newText.length() > 0) {
                    JSONArray searchResult = new JSONArray();
                    for (int i = 0; i < mAllCodes.length(); i ++)
                    {
                        try {
                            JSONObject equipmentCodeInfo = mAllCodes.getJSONObject(i);
                            String strName = equipmentCodeInfo.getString("equipment_name");
                            String strCode = equipmentCodeInfo.getString("equipment_code");
                            if (strName.toLowerCase().contains(newText.toLowerCase()) || strCode.toLowerCase().contains(newText.toLowerCase()))
                            {
                                searchResult.put(equipmentCodeInfo);
                            }
                        }catch (JSONException e)
                        {

                        }
                    }
                    mSearchResult = searchResult;
                    codeListAdapter = new CodeListAdapter(CodeInfoActivity.this,searchResult);
                    mShowCodeListView.setAdapter(codeListAdapter);
                }
                else
                {
                    mSearchResult = mAllCodes;
                    codeListAdapter = new CodeListAdapter(CodeInfoActivity.this,mAllCodes);
                    mShowCodeListView.setAdapter(codeListAdapter);
                }
                return true;
            }
        });

        mSearchEquipmentCodeView.setOnCloseListener(new SearchView.OnCloseListener() {

            @Override
            public boolean onClose() {
                return true;
            }
        });
    }

    public  void  initActionBar(){
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("");
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowCustomEnabled(true);
            LayoutInflater inflator = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflator.inflate(R.layout.actionbar_custom,new LinearLayout(CodeInfoActivity.this),false);
            android.support.v7.app.ActionBar.LayoutParams layout = new android.support.v7.app.ActionBar.LayoutParams(
                    android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT, android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT);
            actionBar.setCustomView(v, layout);
            Toolbar parent = (Toolbar) v.getParent();
            parent.setContentInsetsAbsolute(0, 0);
        }
        TextView actionBarTitle = (TextView) findViewById(R.id.action_bar_title);
        actionBarTitle.setText("设备编码");
        ImageView title_logo = (ImageView)findViewById(R.id.title_logo_id);
        title_logo.setImageResource(R.drawable.quexian_title);
        title_logo.setAlpha(0.7f);
        RelativeLayout bar_back_btn =  (RelativeLayout) findViewById(R.id.bar_back_btn);
        bar_back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CodeInfoActivity.this.finish();
            }
        });
    }

    private static class CodeListAdapter extends BaseAdapter {

        private JSONArray listTag = null;
        private LayoutInflater mLayoutInflater;
        Context mContext;


        public CodeListAdapter(Context context, JSONArray listTag) {
            this.listTag = listTag;
            mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mContext = context;
        }

        @Override
        public boolean isEnabled(int position) {
            return super.isEnabled(position);
        }

        @Override
        public int getCount() {
            return listTag.length();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = mLayoutInflater.inflate(R.layout.code_item, null);
            TextView name_item_text = (TextView) view.findViewById(R.id.name_item_text);
            TextView code_item_text = (TextView) view.findViewById(R.id.code_item_text);
            try {
                JSONObject equipment = listTag.getJSONObject(position);
                name_item_text.setText(equipment.getString("equipment_name"));
                code_item_text.setText(equipment.getString("equipment_code"));
            } catch (JSONException e) {
                Log.e("========JSONException==", e.toString());
                e.printStackTrace();
            }
            return view;
        }


    }
}
